package com.chuji.crmcj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.CancelUserListInfo;
import com.chuji.crmcj.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by y.yiwen on 3/1/2016.
 */
public class SalesAdapter extends BaseAdapter {
    private Context context;
    //驳回人员列表存储
    List<CancelUserListInfo.ApiParamObjEntity> cancelUserListData;
    //驳回人员列表网络数据
    private CancelUserListInfo cancelUserListInfo;

    // 用来控制CheckBox的选中状况
    private HashMap<Integer, Boolean> isSelected;
    //获取销售员ID
    private String SalesID;
    public SalesAdapter(Context context,List<CancelUserListInfo.ApiParamObjEntity> cancelUserListData) {
        isSelected = new HashMap<Integer, Boolean>();
        this.context = context;
        this.cancelUserListData = cancelUserListData;
        // 初始化数据
        initDate();
    }
    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < cancelUserListData.size(); i++) {
            getIsSelected().put(i, false);
        }
    }
    @Override
    public int getCount() {
        return cancelUserListData.size();
    }

    @Override
    public Object getItem(int position) {
        return cancelUserListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.dialog_reject_to_sales, null);
            holder = new ViewHolder();
            holder.mLl_genjinfangshi = (LinearLayout) convertView.findViewById(R.id.ll_genjinfangshi);
            holder.mIv_sales_head = (ImageView) convertView.findViewById(R.id.iv_sales_head);
            holder.mTv_device_name = (TextView) convertView.findViewById(R.id.tv_device_name);
            holder.mCb_familycondition = (CheckBox) convertView.findViewById(R.id.cb_familycondition);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mIv_sales_head.setImageResource(R.drawable.morentouxiang);
        if(cancelUserListData.get(position).getOriginal().equals("1")){
            holder.mTv_device_name.setText(cancelUserListData.get(position).getRealName()+"（原负责人）");
        }else{
            if(cancelUserListData.get(position).getOriginal()!="0"){
                holder.mTv_device_name.setText(cancelUserListData.get(position).getRealName());
            }
        }
        holder.mCb_familycondition.setClickable(false);
        holder.mLl_genjinfangshi.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                isSelected.put(position, false);
                // 遍历list的长度，将MyAdapter中的map值全部设为true
                for (int i = 0; i < cancelUserListData.size(); i++) {
                    if (i == position) {
                        isSelected.put(i, true);
                        SalesID=cancelUserListData.get(i).getID();
                        setSalesID(SalesID);
                    } else {
                        isSelected.put(i, false);
                    }
                    setIsSelected(isSelected);
                    notifyDataSetChanged();
                }
                setIsSelected(isSelected);
            }
        });
        // 根据isSelected来设置checkbox的选中状况
        holder.mCb_familycondition.setChecked(getIsSelected().get(position));
        return convertView;
    }

    public HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        this.isSelected = isSelected;
    }
    public void setSalesID(String SalesID) {
        this.SalesID = SalesID;
    }
    public  String getSalesID() {

        return SalesID;
    }
    class ViewHolder {
        private LinearLayout mLl_genjinfangshi;
        private ImageView mIv_sales_head;
        private TextView mTv_device_name;
        private CheckBox mCb_familycondition;
    }
}
